/*
 * Copyright (c) 2008, Solido Systems
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of Solido Systems nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.solidosystems.objectkitchen.data;

import java.util.*;
import java.io.*;

public class VHash extends Value implements Map<String,Value>{
	private Map<String,Value> internalHash;
	
	public VHash(){
		internalHash=new HashMap<String,Value>();
	}
	
	public boolean equalsSameType(Value val){
		return equals(val);
	}
	
	public void writeToStream(DataOutputStream stream) throws IOException{
		stream.writeByte(Value.HASH);
		stream.writeInt(internalHash.size());
		for(String key:internalHash.keySet()){
			stream.writeUTF(key);
			Value val=internalHash.get(key);
			// stream.writeByte(val.getTypeId());
			val.writeToStream(stream);
		}
	}
	
	public static Value readDataFromStream(DataInputStream stream) throws IOException{
		int size=stream.readInt();
		VHash hash=new VHash();
		for(int i=0;i<size;i++){
			String key=stream.readUTF();
			hash.put(key,Value.readFromStream(stream));
		}
		return hash;
	}
	
	public void clear(){
		internalHash.clear();
	}
	
	public void mergeOver(VHash source){
		for(String key:source.keySet()){
			internalHash.put(key,source.get(key));
		}
	}
	
	public void mergeUnder(VHash source){
		for(String key:source.keySet()){
			if(!internalHash.containsKey(key)){
				internalHash.put(key,source.get(key));
			}
		}
	}
	
	public boolean containsKey(Object key){
		return internalHash.containsKey(key);
	}
	
	public boolean containsValue(Object value){
		return internalHash.containsValue(value);
	}
	
	public Set<Map.Entry<String,Value>> entrySet(){
		return internalHash.entrySet();
	}
	
	public boolean equals(Object obj){
		if(obj.getClass().equals(this.getClass())){
			return internalHash.equals(((VHash)obj).internalHash);
		}
		return false;
	}
	
	public Value get(Object key){
		return internalHash.get(key);
	}
	
	public int hashCode(){
		return internalHash.hashCode();
	}
	
	public boolean isEmpty(){
		return internalHash.isEmpty();
	}
	
	public Set<String> keySet(){
		return internalHash.keySet();
	}
	
	public Value put(String key,Value value){
		return internalHash.put(key,value);
	}
	
	public Value put(String key,String value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,int value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,long value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,float value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,double value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,boolean value){
		return internalHash.put(key,Value.create(value));
	}
	
	public Value put(String key,byte[] value){
		return internalHash.put(key,Value.create(value));
	}
	
	public void putAll(Map<? extends String,? extends Value> t){
		internalHash.putAll(t);
	} 
	
	public Value remove(Object key){
		return internalHash.remove(key);
	}
	
	public int size(){
		return internalHash.size();
	}
	
	public Collection<Value> values(){
		return internalHash.values();
	}
	
	// Value abstracts
	
	public String getTypeName(){
		return "Hash";
	}
	
	public int getTypeId(){
		return HASH;
	}
	
	public String getString(String key){
		Value val=get(key);
		if(val!=null)return val.toString();
		return null;
	}
	
	public Long getLong(String key){
		Value val=get(key);
		if(val!=null)return val.toLong();
		return null;
	}
	
	public Integer getInteger(String key){
		Value val=get(key);
		if(val!=null)return val.toInteger();
		return null;
	}
	
	public Float getFloat(String key){
		Value val=get(key);
		if(val!=null)return val.toFloat();
		return null;
	}
	
	public Double getDouble(String key){
		Value val=get(key);
		if(val!=null)return val.toDouble();
		return null;
	}
	
	public Boolean getBoolean(String key){
		Value val=get(key);
		if(val!=null)return val.toBoolean();
		return null;
	}
	
	public VList getList(String key){
		Value val=get(key);
		if(val!=null)return (VList)val;
		return null;
	}
	
	public byte[] getByteArray(String key){
		Value val=get(key);
		if(val!=null)return val.toByteArray();
		return null;
	}
	
	public String toString(){
		StringBuilder buf=new StringBuilder();
		buf.append("{");
		for(String str:keySet()){
			if(buf.length()>1)buf.append(",");
			buf.append(str);
			buf.append(":");
			buf.append(get(str).toString());
		}
		buf.append("}");
		return buf.toString();
	}
	
	public Integer toInteger(){
		return null;
	}
	
	public Long toLong(){
		return null;
	}
	
	public Float toFloat(){
		return null;
	}
	
	public Double toDouble(){
		return null;
	}
	
	public Boolean toBoolean(){
		return null;
	}
	
	public byte[] toByteArray(){
		return null;
	}
}